package meta;


public class MetaSegment implements HasMeta {
	public float x1,y1,z1,x2,y2,z2;
	public float innerRadius1=0;
	public float innerRadius2=0;
	public float maxDistance=30;
	public float force=1;
	
	public MetaSegment(float x1, float y1, float z1, float x2, float y2, float z2) {
		this.x1 = x1;
		this.y1 = y1;
		this.z1 = z1;
		this.x2 = x2;
		this.y2 = y2;
		this.z2 = z2;
		
	}
	// dist_Point_to_Segment(): get the distance of a point to a segment
//  Input:  a Point P and a Segment S (in any dimension)
//  Return: the shortest distance from P to S
	
	
	public float dist(float x, float y, float z) {
		// TODO Auto-generated method stub
		// dist_Point_to_Segment(): get the distance of a point to a segment
		// Input: a Point P and a Segment S (in any dimension)
		// Return: the shortest distance from P to S
			float dx = x - x1;
			float dy = y - y1;
			float dz = z - z1;
			float vx=x2-x1;
			float vy=y2-y1;
			float vz=z2-z1;
		
			float c1 = dot(dx, dy, dz, vx, vy, vz);
			if (c1 <= 0){
				float cD= dist(x, y, z, x1, y1, z1);
				if (cD<innerRadius1)return 0;
				return cD-innerRadius1;
			}
			float c2 = dot(vx, vy, vz, vx, vy, vz);
			if (c2 <= c1){
				float cD= dist(x, y, z, x2, y2, z2);
				if (cD<innerRadius2)return 0;
				return cD-innerRadius2;
			}
				
			
			float b = c1 / c2;
			float px = x1 + b * vx;
			float py = y1 + b * vy;
			float pz = z1 + b * vz;
			float deltaR=innerRadius2-innerRadius1;
			float r=innerRadius1+b*deltaR;
			
			float cD=dist(x, y, z, px, py, pz);
			if (cD<r)return 0;
			return cD-r;
			//return dist(x, y, z, px, py, pz);
		
	}

	public float map(float v,float v1,float v2,float r1,float r2){
		float d1=v2-v1;
		float d2=r2-r1;
		return d2*(v-v1)/d1+r1;
	}

	@Override
	public float getMeta(float x, float y, float z) {
		// TODO Auto-generated method stub
	//System.out.println(getDistanceSqr(x,y,z));
		float cD=dist(x,y,z);
		cD=Math.min(cD,maxDistance);
		//return map(cD,0,100,1,0);
		/*float meta=force/(cD*cD+1f);
		if (meta<0)System.out.println(meta);
		return meta;*/
		return force/(cD+1);
	}
	public static float distSqr(float x1, float y1, float z1, float x2, float y2,
			float z2) {
		float dX = x2 - x1;
		float dY = y2 - y1;
		float dZ = z2 - z1;
		return dX * dX + dY * dY + dZ * dZ;
	}

	public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) {
		return (float) Math.sqrt(distSqr(x1, y1, z1, x2, y2, z2));
	}
	public static float dot(float x1, float y1, float z1, float x2, float y2,
			float z2) {
		return x1 * x2 + y1 * y2 + z1 * z2;
	}
}
